/**
 * app交互协议
 */

export default {
  goScan(callback) {
    window.h5scan = callback
    window.location.href = "wccia://scan/h5scan"
  },
  registerLifecircle(callback) {
    window.lifecircle = callback
  },
  registerLogin: function(callback) {
    window.logined = callback
    window.location.href = "wccia://register/login"
  },
  notifyLogined: function(params) {
    window.location.href = "wccia://notify/login/logined/"+ params
  },
  /**
   * 检测登录状态，未登录时弹出登录页面
   * @param callback: 登录成功后的回调，用于接收urid
   */
  checkLogin: function (callback) {
    window.wcclogincallback = callback
    window.location.href = "wccia://login/wcclogincallback"
  },

  getAddress: function (callback) {
    window.wccAddressCallBack = callback
    window.location.href = "wccia://address/wccAddressCallBack"
  },
  /**
   * 让app打开一个新的web页面
   * @param url: 新页面的相对路径
   * @param newParameter: 新页面需要增加的参数,类型为json对象，例如：{'a':'001','b':'002'}
   */
  openNewWebView: function (vue, url, newParameter) {
    var newUrl = location.protocol + "//" + location.host + url + '?'

    if (newParameter) {
      for (var item in newParameter) {
        newUrl = newUrl + item + "=" + newParameter[item] + "&"
      }
      newUrl = newUrl.substring(0, newUrl.length - 1)
    }
    location.href = "wccia://webview/" + newUrl
  },
  /**
   * 跳转native页面
   * @param jumpid: native页面对应编号
   * @param parameter: 跳转时需要附带的参数, 类型为json对象，例如：{'a':'001','b':'002'}
   */
  jumpToNative: function (jumpid, parameter) {
    var url = "wccia://jump/" + jumpid
    if (parameter) {
      for (var item in parameter) {
        url = url + "/" + item + "=" + parameter[item]
      }
    }
    location.href = url
  },
  /**
   * 获取客户端基本参数
   * @param callback: 获取到参数后的回调，用于接受参数，参数为json对象
   */
  getNativeParameter: function (callback) {
    window.wccparametercallback = callback
    window.location.href = "wccia://getuserparams/wccparametercallback"
  },
  /**
   * 关闭当前webview
   */
  closeWebView: function () {
    location.href = "wccia://close/"
  },
  /**
   * 分享
   * @param title
   * @param content
   * @param url
   * @param imgUrl
   * @param isNative true:显示App右上角分享按钮并设置参数 ；false：发起分享
   */
  share(title, content, url, imgUrl, isNative) {
    let hrefUrl = ''
    if (isNative === true) {
      hrefUrl += 'wccia://showshare/'
    } else {
      hrefUrl += 'wccia://share/'
    }
    hrefUrl += encodeURIComponent(imgUrl) +
      '/' + encodeURIComponent(title) +
      '/' + encodeURIComponent(content + url) +
      '/' + encodeURIComponent(url)
    location.href = hrefUrl
  }


}
